namespace SimpleWebServices.Web.UI.Html
{
    using System;
    using System.Xml;
    using System.Xml.Schema;
    using System.Xml.Serialization;
    using SimpleWebServices.Globalization;

    [XmlRoot("element")]
    public abstract class Element : IElement, IXmlSerializable
    {
        public Names Class
        {
            get;
            set;
        }

        public Name Id
        {
            get;
            set;
        }

        public Language Language
        {
            get;
            set;
        }

        public string OnClick
        {
            get;
            set;
        }

        public string Title
        {
            get;
            set;
        }

        public virtual IElement Deserialize(string name, string type, string xml)
        {
            return null;
        }

        public virtual string Render()
        {
            return this.Serialize().CreateNavigator().OuterXml;
        }

        public IElement ToElement()
        {
            return this;
        }

        public override int GetHashCode()
        {
            return this.ToString().GetHashCode();
        }

        public override string ToString()
        {
            return this.Render();
        }

        public XmlSchema GetSchema()
        {
            throw new NotSupportedException();
        }

        public virtual void ReadXml(XmlReader reader)
        {
            if (null == reader)
            {
                throw new ArgumentNullException("reader");
            }

            if (XmlNodeType.Element == reader.NodeType)
            {
                string value = reader.GetAttribute("id");
                if (!string.IsNullOrEmpty(value))
                {
                    this.Id = value;
                }

                value = reader.GetAttribute("class");
                if (!string.IsNullOrEmpty(value))
                {
                    this.Class = value;
                }

                value = reader.GetAttribute("lang");
                if (!string.IsNullOrEmpty(value))
                {
                    this.Language = value;
                }

                value = reader.GetAttribute("title");
                if (!string.IsNullOrEmpty(value))
                {
                    this.Title = value;
                }

                value = reader.GetAttribute("onclick");
                if (!string.IsNullOrEmpty(value))
                {
                    this.OnClick = value;
                }
            }
        }

        public virtual void WriteXml(XmlWriter writer)
        {
            if (null == writer)
            {
                throw new ArgumentNullException("writer");
            }

            if (null != this.Id)
            {
                writer.WriteAttributeString("id", this.Id);
            }

            if (null != this.Class)
            {
                writer.WriteAttributeString("class", this.Class);
            }

            if (null != this.Language)
            {
                writer.WriteAttributeString("lang", this.Language);
            }

            if (!string.IsNullOrEmpty(this.Title))
            {
                writer.WriteAttributeString("title", this.Title);
            }

            if (!string.IsNullOrEmpty(this.OnClick))
            {
                writer.WriteAttributeString("onclick", this.OnClick);
            }
        }
    }
}